package me.zhengjie.org.centerbase.service.impl;

import me.zhengjie.org.centerbase.domain.CenterBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.org.centerbase.repository.CenterBaseRepository;
import me.zhengjie.org.centerbase.service.CenterBaseService;
import me.zhengjie.org.centerbase.service.dto.CenterBaseDto;
import me.zhengjie.org.centerbase.service.dto.CenterBaseQueryCriteria;
import me.zhengjie.org.centerbase.service.mapper.CenterBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "centerBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CenterBaseServiceImpl implements CenterBaseService {

    private final CenterBaseRepository centerBaseRepository;

    private final CenterBaseMapper centerBaseMapper;

    public CenterBaseServiceImpl(CenterBaseRepository centerBaseRepository, CenterBaseMapper centerBaseMapper) {
        this.centerBaseRepository = centerBaseRepository;
        this.centerBaseMapper = centerBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CenterBaseQueryCriteria criteria, Pageable pageable){
        Page<CenterBase> page = centerBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(centerBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CenterBaseDto> queryAll(CenterBaseQueryCriteria criteria){
        return centerBaseMapper.toDto(centerBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CenterBaseDto findById(Integer id) {
        CenterBase centerBase = centerBaseRepository.findById(id).orElseGet(CenterBase::new);
        ValidationUtil.isNull(centerBase.getId(),"CenterBase","id",id);
        return centerBaseMapper.toDto(centerBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CenterBaseDto create(CenterBase resources) {
        return centerBaseMapper.toDto(centerBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CenterBase resources) {
        CenterBase centerBase = centerBaseRepository.findById(resources.getId()).orElseGet(CenterBase::new);
        ValidationUtil.isNull( centerBase.getId(),"CenterBase","id",resources.getId());
        centerBase.copy(resources);
        centerBaseRepository.save(centerBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            centerBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CenterBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CenterBaseDto centerBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("所属部门", centerBase.getDepartmentId());
            map.put("所属部门", centerBase.getDepartmentId().getDepartmentName());
            map.put("中心名称", centerBase.getCenterName());
            map.put("创建时间", centerBase.getStartTime());
            map.put("撤销时间", centerBase.getEndTime());
            map.put("中心描述", centerBase.getDescribeInfo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}